package com.woniuxy.hotel.room.controller;

import com.woniuxy.hotel.room.pojo.Room;
import com.woniuxy.hotel.room.pojo.RoomType;
import com.woniuxy.hotel.room.service.RoomService;
import com.woniuxy.hotel.room.service.RoomTypeService;
import com.woniuxy.hotel.room.util.LayUiDataResultDTO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/room")
public class RoomController {
    
    @Resource
    private RoomService roomServiceImp;
    @Resource
    private RoomTypeService roomTypeService;
    
    /*查找所有房间
     *
     *
     * */
    @RequestMapping("/findAllRoom")
    @ResponseBody
    public List<Room> findAllRoom () {
        
        return roomServiceImp.findAllRoom();
        
    }
    
    
    /*动态SQl查找房间
     * 所有字段都支持
     * 前端显示房间类型名称,传value为房间类型ID编号
     * */
    @RequestMapping("/findRoom1")
    @ResponseBody
    public List<Room> findRoom1 (Room room, BigDecimal low, BigDecimal high) {
        return roomServiceImp.findRoom1(room, low, high);
        
    }
    
    /*动态SQl增加房间*/
    @RequestMapping("/insert")
    public int insertRoom (Room room, boolean breakfast, boolean window) {
        String a = "";
        if (breakfast) {
            a += "含早,";
        } else {
            a += "不含早,";
        }
        if (window) {
            a += "落地窗";
        } else {
            a += "小窗";
        }
        room.setRoomtrait(a);
        
        RoomType roomType=new RoomType();
        roomType.setRoomtypeid(room.getRoomtypeid());
        roomType.setRoomprice(room.getRoomprice());
        roomTypeService.updateByPrimaryKey(roomType);
    
        roomServiceImp.updateByRoomTypeId(room);
        return roomServiceImp.insertRoom(room);
    }
    
    /*
     *
     * 根据房间ID删除房间*/
    @RequestMapping("/deleteId")
    public String delRoom (Room room) {
        String s = "删除失败,没有相应房间";
        int a = roomServiceImp.delRoom(room);
        if (a == 1) {
            s = "删除成功";
            return s;
        }
        return s;
    }
    
    /*
     *
     * 动态Sql删除房间*/
    @RequestMapping("/deleteSql")
    public int delRoomSql (Room room) {
        return roomServiceImp.delRoomSql(room);
    }
    
    /*
     * 对所有房间进行动态Sql修改
     * 动态Sql修改房间*/
    @RequestMapping("/updateAll")
    public int updateRoomSql (Room room) {
        return roomServiceImp.updateRoomSql(room);
    }
    
    /*
     * 根据主键
     * 对相应房间进行Sql修改*/
    @RequestMapping("/updateOneAll")
    public int updateRoomSql1 (Room room, boolean breakfast, boolean window) {
        String a = "";
        if (breakfast) {
            a += "含早,";
        } else {
            a += "不含早,";
        }
        if (window) {
            a += "落地窗";
        } else {
            a += "小窗";
        }
        room.setRoomtrait(a);
    
        RoomType roomType=new RoomType();
        roomType.setRoomtypeid(room.getRoomtypeid());
        roomType.setRoomprice(room.getRoomprice());
        roomTypeService.updateByPrimaryKey(roomType);
        
        roomServiceImp.updateByRoomTypeId(room);
        return roomServiceImp.updateRoomSql1(room);
    }
    
    @RequestMapping("/allRoomInfo")
    public LayUiDataResultDTO allRoomInfo (Integer page, Integer limit) {
        return new LayUiDataResultDTO(0, "", roomServiceImp.selectAllRoomsInfo(page, limit), roomServiceImp.countNum(0));
    }
    
    @RequestMapping("/deleted")
    public LayUiDataResultDTO deletedRoomInfo(Integer page,Integer limit){
        return new LayUiDataResultDTO(0, "", roomServiceImp.selectDeletedRoomsInfo(page, limit), roomServiceImp.countNum(1));
    
    }
    
    /**
     * 恢复房间
     */
    @RequestMapping("/recoverId")
    public String recoverRoom (Integer roomid) {
        String s = "恢复失败,没有相应房间";
        int a = roomServiceImp.recoverRoom(roomid);
        if (a == 1) {
            s = "恢复成功";
        }
        return s;
    }
    
    /*
     *
     * 更具酒店Id
     * 查找这个酒店的所有房间
     * */
    @RequestMapping("/findAllRoomThisHotel")
    @ResponseBody
    public List<Room> findAllRoomThisHotel(Room room){
        
        return roomServiceImp.findAllRoomThisHotel(room);
        
    }
    
    
}
